﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcMusicStore.Helpers
{
    public static class HtmlHelpers
    {
        public static string Truncate(this HtmlHelper helper, string input, int maxLenght)
        {
            if (maxLenght <= 0)
            {
                return input;
            }

            if (input.Length <= maxLenght)
            {
                return input;
            }
            else
            {
                return input.Substring(0,maxLenght)+ "...";
            }
        } 
        
        /// <summary>
        /// 获取控制器的名称
        /// </summary>
        /// <typeparam name="T">控制器类型</typeparam>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string GetControllerName<T>(this HtmlHelper helper) where T:Controller
        {
            string typeName = typeof(T).Name;
            if (typeName.EndsWith("Controller"))
            {
                typeName = typeName.Substring(0, typeName.LastIndexOf("Controller"));
            }
            return typeName;
        }
        
        /// <summary>
        /// 获取控制器的名称
        /// </summary>
        /// <typeparam name="T">控制器类型</typeparam>
        /// <returns></returns>
        public static string GetControllerName<T>() where T : Controller
        {
            string typeName = typeof(T).Name;
            if (typeName.EndsWith("Controller"))
            {
                typeName = typeName.Substring(0, typeName.LastIndexOf("Controller"));
            }
            return typeName;
        }
    }

   
}